/**
 * Created by xiebang
 * Description
 * User:小谢
 * Date:2021-09-26
 * Time:21:36
 */
public class MyCircularQueue {
    private int[] elem;
    private int usedSize;
    private int front;
    private int rear;

    public MyCircularQueue(int k) {
        this.elem = new int[k];
    }

    /**
     * 入队
     * @param value
     * @return
     */
    public boolean enQueue(int value) {
        if(isFull()) {
            return false;
        }
        this.elem[this.rear] = value;
        this.rear = (this.rear+1) % this.elem.length;
        return true;
    }

    public boolean isFull() {
        if( (this.rear+1) % this.elem.length == this.front) {
            return true;
        }
        return false;
    }


    /**
     * 出队
     * @return
     */
    public boolean deQueue() {
        if(isEmpty()) {
            return false;
        }
        this.front = (this.front+1) % this.elem.length;
        return true;
    }

    public boolean isEmpty() {
        if(this.front == this.rear){
            return true;
        }
        return false;
    }

    /**
     * 得到队头元素  相当于 peek()
     * @return
     */
    public int Front() {
        if(isEmpty()) {
            return -1;
        }
        int val = this.elem[this.front];
        //this.front = (this.front+1) % this.elem.length; 不能删除的
        return val;
    }

    /**
     * 得到队尾元素
     * @return
     */
    public int Rear() {
        if(isEmpty()) {
            return -1;
        }
        if(this.rear == 0) {
            return this.elem[this.elem.length-1];
        }
        return this.elem[this.rear-1];
    }
}
